package com.edu.web.servlet;

import com.edu.bean.User;
import com.edu.dao.UserDao;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.lang.System.out;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String userName=req.getParameter("username");
        String userPass=req.getParameter("password");

        UserService us=new UserServiceImpl();
        User user=us.userLogin(userName,userPass);

        if(user.getUserId()==-2){
            req.setAttribute("baocuo","账号不存在");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        if(user.getUserId()==-1){
            req.setAttribute("baocuo","密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        req.getSession().setAttribute(userName,userPass);
        req.getRequestDispatcher("LoadServlet").forward(req,resp);
    }
}
